const Router = require("@koa/router");
const { OrderModel,TravelModel} = require("../database/index");
const router = new Router();
const { Op } = require('sequelize');
const sequelize = require('../database/index')

// 获取景点名称
router.get("/gettravel", async (ctx, next) => {
  try {
    const result = await TravelModel.findAll();
    console.log(result);

    ctx.body = {
      code: 200,
      msg: "获取数据成功!!!",
      data: result,
    };
  } catch (error) {
    ctx.body = {
      code: 500,
      msg: "获取数据失败",
    };
  }
});
// 查询（分页，条件）
router.get("/getSpecificData", async (ctx, next) => {  
  const { page = 1, pageSize = 5, name, date } = ctx.query; // 默认第一页，每页5条数据  
  const offset = (page - 1) * pageSize;
  const pageSizeNum = +pageSize
    
  let whereClause = {};  
  if (name) {  
    whereClause.name = { [Op.like]: `%${name}%` }; // 模糊匹配  
  }  
  if (date) {  
    // 构造日期范围  
    const startDate = new Date(date + 'T00:00:00');  
    const endDate = new Date(date + 'T23:59:59');  
    whereClause.createdAt = { [Op.between]: [startDate, endDate] };  
  }  
  console.log('111111111111',pageSize,offset);
  
  
  try {  
    const result = await OrderModel.findAll({  
      limit: pageSizeNum,  
      offset: offset,  
      where: whereClause,  
    });  
  
    // 计算总记录数前台显示总页数  
    const totalCount = await OrderModel.count({ where: whereClause });  
    const totalPages = Math.ceil(totalCount / pageSize);  
  
    ctx.body = {  
      code: 200,  
      msg: "获取数据成功!!!",  
      data: result,  
      meta: {  
        page: page,  
        pageSize: pageSize,  
        totalPages: totalPages,  
        totalCount: totalCount,  
      },  
    };  
  } catch (error) {  
    ctx.body = {  
      code: 500,  
      msg: "获取数据失败",  
    }; 
    console.log(error);
     
  }  
});

// 添加订单

router.post("/addorder", async (ctx, next) => {  
  const { travelId, name, number, description, content } = ctx.request.body; // 添加 travelId  
  console.log("测试", travelId, name, number, description, content);  
  
  try {  
    const result = await OrderModel.create({  
      travelId, // 添加 travelId 到创建订单的字段中  
      name,  
      number,  
      description,  
      content  
    });  
  
    console.log(result.toJSON());  
    ctx.body = {  
      code: 200,  
      msg: "添加成功",  
      data: result.toJSON() 
    };  
  } catch (error) {   
    ctx.body = {  
      code: 500,  
      msg: "添加失败",    
      error: "添加订单时发生错误，请稍后再试。"   
    };  
    console.error(error);  
  }  
});

// 删除订单
router.post('/deleteOrder', async (ctx, next) => {
    const { orderlId } = ctx.request.body;

    try {
        const result = await OrderModel.destroy({
            where: {orderlId}
        });

        if (result) {
            ctx.body = {
                code: 200,
                msg: "删除成功"
            };
        } else {
            ctx.body = {
                code: 201,
                msg: "删除失败"
            };
        }
    } catch (error) {
        ctx.body = {
            code: 500,
            msg: "删除失败"
        };
    }
});
  
module.exports = router;
